<?php
/**
 * 客户后台 整体妆容类别的管理
 * Author: lyun
 * Date: 2018/6/25
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\Admin\AdminApiController;
use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\PackCategory\PackCategoryIndexRequest;
use App\Http\Requests\Admin\Customer\PackCategory\PackCategoryStoreRequest;
use App\Http\Requests\Admin\Customer\PackCategory\PackCategoryUpdateRequest;
use App\Models\Admin\CustomerAdmin;
use App\Models\Store\PackCategory;
use App\Services\Utils\Guard;
use App\Services\Utils\RequestParam;
use App\Services\Utils\Transaction;
use Illuminate\Pagination\LengthAwarePaginator;
use Tests\Feature\Admin\Customer\PackCategoryControllerTest;

class PackCategoryController extends AdminApiController
{
    const DEFAULT_ORDER_BYS = [
        [
            'order_by' => 'created_at',
            'sort'     => 'desc',
        ]
    ];

    const ROUTE_NAMES = [
        'index'   => 'admin.customer.pack-category.index',
        'store'   => 'admin.customer.pack-category.store',
        'update'  => 'admin.customer.pack-category.update',
        'destroy' => 'admin.customer.pack-category.destroy',
    ];

    /**
     * 显示/查询所有整体妆容类别
     * @test
     *
     * @param PackCategoryIndexRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(PackCategoryIndexRequest $request)
    {
        $this->checkCustomer();

        $this->checkGuardIsCustomerSuperAdmin();

        $customer_id = Guard::customerAdmin()->customer_id;

        /** @var LengthAwarePaginator $packs */
        $pack_categories = (new PackCategory)
            ->where(compact('customer_id'))
            ->orderBys($request->order_bys, self::DEFAULT_ORDER_BYS)
            ->paginate($request->per_page ?: 15);

        return $this->response(compact('pack_categories'));
    }

    /**
     * 新增 整体妆容类别
     * @test
     *
     * @param PackCategoryStoreRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(PackCategoryStoreRequest $request)
    {
        $this->checkCustomer();

        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        $pack_data = [
            'customer_id' => Guard::customerAdmin()->customer_id,
            'name'        => $request->name,
            'priority'    => $request->priority,
            'image'       => $request->image,
        ];

        $stored = Transaction::handle(function () use ($pack_data) {
            /** @var PackCategory $pack_category */
            $pack_category = (new PackCategory)->create($pack_data);

            return $pack_category;
        });

        return $this->response(compact('stored'));
    }


    /**
     * 更新 整体妆容类别
     * @test
     *
     * @param PackCategoryUpdateRequest $request
     * @param $pack_category_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(PackCategoryUpdateRequest $request, $pack_category_id)
    {
        $this->checkCustomer();

        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        $customer_id = Guard::customerAdmin()->customer_id;

        /** @var PackCategory $pack_category */
        $pack_category = (new PackCategory)->where(compact($customer_id))
            ->find($pack_category_id);
        $this->checkItemExist($pack_category, trans('pc.pack_category'));

        $pack_data = RequestParam::normalize([
            'name','priority','image'
        ]);

        $updated = Transaction::handle(function () use ($pack_category, $pack_data) {
            $pack_category->update($pack_data);

            return $pack_category;
        });

        return $this->response(compact('updated'));
    }

    /**
     * 删除 整体妆容类别
     * @test
     *
     * @param $pack_category_id
     * @return array|\Illuminate\Http\Response
     * @throws /Exception
     */
    public function destroy($pack_category_id)
    {
        $this->checkCustomer();

        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        $customer_id = Guard::customerAdmin()->customer_id;

        /** @var PackCategory $pack_category */
        $pack_category = (new PackCategory)
            ->where(compact($customer_id))
            ->find($pack_category_id);

            $this->checkItemExist($pack_category, trans('pc.pack_category'));
            $this->checkUnless($pack_category->packs()->exists(), trans('pc.pack_category_used'));

        $deleted = $pack_category->delete();

        return $this->response(compact('deleted'));
    }
}
